﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace Chizx.Web.user
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["UserEntity"])
            {
                Response.Redirect("/e/Login.aspx");
            }
            if (!Page.IsPostBack)
            {
                string zu_id = "";
                if (Request.Params["id0"] != null && Request.Params["id0"].Trim() != "")
                {
                    zu_id = Request.Params["id0"];
                }
                int zc_id = -1;
                if (Request.Params["id1"] != null && Request.Params["id1"].Trim() != "")
                {
                    zc_id = (Convert.ToInt32(Request.Params["id1"]));
                }
                ShowInfo(zu_id, zc_id);
            }
        }

        private void ShowInfo(string zu_id, int zc_id)
        {
            Chizx.BLL.UserBll bll = new Chizx.BLL.UserBll();
            Chizx.Model.UserModel model = bll.GetModel(zu_id, zc_id);
            this.lblzu_id.Text = model.zu_id;
            this.lblzc_id.Text = model.zc_id.ToString();
            this.txtzu_name.Text = model.zu_name;
            this.txtzu_pwd.Text = model.zu_pwd;
            this.txtzu_pwd2.Text = model.zu_pwd;
            this.txtzu_roles.Text = model.zu_roles == "SS" ? "学生" : (model.zu_roles == "SX" ? "实习生" : (model.zu_roles == "A1" ? "指导老师" : (model.zu_roles == "A2" ? "院系负责人" : (model.zu_roles == "A3" ? "教务管理员" : (model.zu_roles == "A0" ? "系统管理员" : "非法")))));
            this.txtzu_weixin.Text = model.zu_weixin;
            this.txtzu_qq.Text = model.zu_qq;
            this.txtzu_tel.Text = model.zu_tel;
            this.txtzu_email.Text = model.zu_email;
            this.txtzu_lastdate.Text = model.zu_lastdate == null ? "从未登录" : model.zu_lastdate;
            this.txtzu_lastip.Text = model.zu_lastip == null ? "从未登录" : model.zu_lastip;
            this.txtzu_createdate.Text = model.zu_createdate.ToString();
            this.txtzu_status.Text = model.zu_status == "u0" ? "在校中" : (model.zu_status == "u1" ? "实习中" : "未知状态");
            this.txtzu_direct.Text = model.zu_direct == "00" ? "暂未分配" : model.zu_direct;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            string zu_pwd_this;
            if (Request.QueryString["tabName"] == null)
            {
                zu_pwd_this = this.txtzu_pwd.Text.Trim();
            }
            else
            {
                zu_pwd_this = this.txtzu_pwd2.Text.Trim();
            }

            string strErr = "";
            if (this.txtzu_name.Text.Trim().Length == 0)
            {
                strErr += "zu_name不能为空！\\n";
            }
            if (zu_pwd_this.Length == 0)
            {
                strErr += "zu_pwd不能为空！\\n";
            }
            if (int.Parse(this.txtzu_sex.SelectedItem.Value) == 3)
            {
                strErr += "请选择性别！\\n";
            }
            if (this.txtzu_roles.Text.Trim().Length == 0)
            {
                strErr += "zu_roles不能为空！\\n";
            }
            if (this.txtzu_weixin.Text.Trim().Length == 0)
            {
                strErr += "zu_weixin不能为空！\\n";
            }
            if (this.txtzu_qq.Text.Trim().Length == 0)
            {
                strErr += "zu_qq不能为空！\\n";
            }
            if (this.txtzu_tel.Text.Trim().Length == 0)
            {
                strErr += "zu_tel不能为空！\\n";
            }
            if (this.txtzu_email.Text.Trim().Length == 0)
            {
                strErr += "zu_email不能为空！\\n";
            }
            if (this.txtzu_lastdate.Text.Trim().Length == 0)
            {
                strErr += "zu_lastdate不能为空！\\n";
            }
            if (this.txtzu_lastip.Text.Trim().Length == 0)
            {
                strErr += "zu_lastip不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtzu_createdate.Text))
            {
                strErr += "zu_createdate格式错误！\\n";
            }
            if (this.txtzu_status.Text.Trim().Length == 0)
            {
                strErr += "zu_status不能为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string zu_id = this.lblzu_id.Text;
            int zc_id = int.Parse(this.lblzc_id.Text);
            string zu_name = this.txtzu_name.Text;
            string zu_pwd = zu_pwd_this;
            int zu_sex = int.Parse(this.txtzu_sex.SelectedItem.Value);
            string zu_roles = this.txtzu_roles.Text == "学生" ? "SS" : (this.txtzu_roles.Text == "实习生" ? "SX" : (this.txtzu_roles.Text == "指导老师" ? "A1" : (this.txtzu_roles.Text == "院系负责人" ? "A2" : (this.txtzu_roles.Text == "教务管理员" ? "A3" : "A0"))));
            string zu_weixin = this.txtzu_weixin.Text;
            string zu_qq = this.txtzu_qq.Text;
            string zu_tel = this.txtzu_tel.Text;
            string zu_email = this.txtzu_email.Text;
            string zu_lastdate = this.txtzu_lastdate.Text;
            string zu_lastip = this.txtzu_lastip.Text;
            DateTime zu_createdate = DateTime.Parse(this.txtzu_createdate.Text);
            string zu_status = this.txtzu_status.Text == "在校中" ? "u0" : (this.txtzu_status.Text == "实习中" ? "u1" :  "u2");
            //MessageBox.Show(this, zu_id + "," + zc_id + "," + zu_name + "," + zu_pwd + "," + zu_sex + "," + zu_roles + "," + zu_lastdate + "," + zu_lastip + "," + zu_createdate + "," + zu_status + ",");
            //return;
            string zu_direct = this.txtzu_direct.Text == "暂未分配" ? "00" : this.txtzu_direct.Text;

            Chizx.Model.UserModel model = new Chizx.Model.UserModel();
            model.zu_id = zu_id;
            model.zc_id = zc_id;
            model.zu_name = zu_name;
            model.zu_pwd = zu_pwd;
            model.zu_sex = zu_sex;
            model.zu_roles = zu_roles;
            model.zu_weixin = zu_weixin;
            model.zu_qq = zu_qq;
            model.zu_tel = zu_tel;
            model.zu_email = zu_email;
            model.zu_lastdate = zu_lastdate;
            model.zu_lastip = zu_lastip;
            model.zu_createdate = zu_createdate;
            model.zu_status = zu_status;
            model.zu_direct = zu_direct;

            //写入系统日志
            if (Session["UserEntity"] == null)
            {
                Response.Redirect("/e/Login.aspx");
            }
            else
            {
                Chizx.BLL.UserBll bll = new Chizx.BLL.UserBll();
                bll.Update(model);
                Chizx.Model.UserModel sbmmodel = (Chizx.Model.UserModel)Session["UserEntity"];
                Chizx.BLL.SystemLogBll sbll = new Chizx.BLL.SystemLogBll();
                Chizx.Model.SystemLogModel slm = new Chizx.Model.SystemLogModel();
                slm.zu_id = sbmmodel.zu_id;
                slm.zl_dowhen = DateTime.Now;
                slm.zl_dowhat = "修改用户信息";
                slm.zl_dotype = "3";//0:Test,1:Auth,2:Read,3:Write,4:Del,5:Inpor
                slm.zl_dodesc = "操作成功";
                sbll.Add(slm);
                Chizx.Model.MessageModel mm = new Chizx.Model.MessageModel();
                Chizx.BLL.MessageBll mb = new Chizx.BLL.MessageBll();
                mm.zu_id = zu_id;
                mm.zmsg_topic = "修改密码成功";
                mm.zmsg_context = "尊敬的用户： 您好！你已经成功修改了"+zu_id+"的用户信息！";
                mm.zmsg_createdate = DateTime.Now;
                mm.zmsg_status = "m0";
                mb.Add(mm);
                //更新Session
                Session["UserEntity"] = model;
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "/companys/list.aspx");
            }
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
